package cn.tedu.supermarket.controller;

import cn.tedu.supermarket.entity.BannersDTO;
import cn.tedu.supermarket.entity.GoodsUser;
import cn.tedu.supermarket.entity.User;
import cn.tedu.supermarket.mapper.BannersMapper;
import cn.tedu.supermarket.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {
    @Resource
    GoodsMapper goodsMapper;
    @Autowired
    BannersMapper bannersMapper;

    //商家方面
    @RequestMapping("/sIndex")
    public String listPrereg(Model model, HttpSession session) {
        if (session.getAttribute("user")!=null){
            List<BannersDTO> banners = bannersMapper.select();
            model.addAttribute("banners",banners);
            List<GoodsUser> list=goodsMapper.findAddtime();
            model.addAttribute("list",list);
            List<GoodsUser> list1 = goodsMapper.findSalesum();
            model.addAttribute("list1",list1);
            return "sel_user/suserIndex";
        }
        return "sel_user/suserLogin";
    }
    @RequestMapping("/_del")
    public String goodsList(Model model,HttpSession session){
        if (session.getAttribute("user")!=null){
            List<GoodsUser> list = goodsMapper.findAddtime();
            model.addAttribute("list",list);
            return "sel_user/deleteGoods";
        }
        return "sel_user/suserLogin";
    }
    @RequestMapping("/sMore")
    public String sMore(Model model,HttpSession session){
        if (session.getAttribute("user")==null){
            return "sel_user/suserLogin";
        }
        List<GoodsUser> list = goodsMapper.selectAll();
        model.addAttribute("list",list);
        return "sel_user/suserMore";
    }
    //商品详情
    @RequestMapping("/sel/detail")
    public String det(int id,Model model,HttpSession session){
        if (session.getAttribute("user")==null){
            return "sel_user/suserLogin";
        }
        GoodsUser goodsUser = goodsMapper.selectById(id);
        model.addAttribute("goodsUser",goodsUser);
        return "sel_user/goodsDetail";
    }

    //用户主页
    @RequestMapping("/bIndex")
    public String bIndex(Model model){
        List<BannersDTO> banners = bannersMapper.select();
        model.addAttribute("banners",banners);
        List<GoodsUser> list=goodsMapper.findAddtime();
        model.addAttribute("list",list);
        List<GoodsUser> list1 = goodsMapper.findSalesum();
        model.addAttribute("list1",list1);
        return "buy_user/buserIndex";
    }
    @RequestMapping("/bMore")
    public String bMore(Model model){
        List<GoodsUser> list = goodsMapper.selectAll();
        model.addAttribute("list",list);
        return "buy_user/buserMore";
    }
    //商品详情
    @RequestMapping("/detail")
    public String details(int id,Model model){
        GoodsUser goodsUser = goodsMapper.selectById(id);
        model.addAttribute("goodsUser",goodsUser);
        return "buy_user/goodsDetail";
    }
}
